package org.geekbang.thinking.in.spring.bean.definition;

import org.geekbang.thinking.in.spring.bean.factory.DefaultUserFactory;
import org.geekbang.thinking.in.spring.bean.factory.UserFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * Bean 初始化 Demo
 *
 * @author <a href="mailto:hushengjin1993@gmail.com">Hu Shengjin</a>
 * @since
 */
@Configuration // Configuration Class
public class BeanInitializationDemo {

    public static void main(String[] args) {
        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration Class (配置类)
        applicationContext.register(BeanInitializationDemo.class);
        // 启动 Spring 应用上下文
        applicationContext.refresh();
        System.out.println("Spring 上下文已启动。。。");
        // 依赖查找
        UserFactory userFactory = applicationContext.getBean(UserFactory.class);
        System.out.println("Spring 上下文准备关闭。。。");
        // 关闭 Spring 应用上下文
        applicationContext.close();
        System.out.println("Spring 上下文已关闭。。。");
    }

    @Bean(initMethod = "initUserFactory", destroyMethod = "doDestroy")
//    @Lazy
    public UserFactory userFactory() {
        return new DefaultUserFactory();
    }

}
